﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Commons.Collections;
using NUnit.Framework;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;

namespace Velocity.Test
{
    [TestFixture]
    public class VelocityInHibernateFixture
    {
        private VelocityEngine engine = new VelocityEngine();

        [SetUp]
        public void SetUp()
        {
            ExtendedProperties props = new ExtendedProperties();

            /// переопределяем загрузчик ресурсов так, чтобы GetTemplate в
            /// качестве параметра принимал сам шаблон а не путь к файлу с ним.
            props.SetProperty(RuntimeConstants_Fields.RESOURCE_LOADER, "class");
            props.SetProperty("class.resource.loader.class", "Velocity.Utils.StringResourceLoader;Velocity.Utils");

            this.engine.Init(props);
        }

        [Test]
        public void SmokeTest()
        {
            Assert.IsNotNull(this.engine);
        }

        [Test]
        public void GetTemplateTest()
        {
            Assert.IsNotNull(this.engine.GetTemplate(Common.TEMPLATE));
        }

        /// <summary>
        /// Получение выходного потока с помощью Context-Template-Merge
        /// </summary>
        [Test]
        public void TemplateMergeTest()
        {
            VelocityContext context = new VelocityContext();
            Template template = this.engine.GetTemplate(Common.TEMPLATE);

            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb);

            DateTime now = DateTime.Now;

            context.Put("name", "John");
            context.Put("surname", "Smith");
            context.Put("templateType", "hibernate_lib_style");
            context.Put("date", now);

            template.Merge(context, writer);

            var expected = String.Format(
                Common.EXPECTED,
                now);
            Assert.AreEqual(expected, sb.ToString());
        }

        [Test]
        public void TemplateMergeWithClassTest()
        {
            VelocityContext context = new VelocityContext();
            Template template = this.engine.GetTemplate(Common.CLASS_TEMPLATE);

            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb);

            DateTime now = DateTime.Now;

            context.Put("clazz", new Common.Struct("John", "Smith", "hibernate_lib_style", now));

            template.Merge(context, writer);

            var expected = String.Format(
                Common.EXPECTED,
                now);
            Assert.AreEqual(expected, sb.ToString());
        }

        [Test]
        public void TemplateMergeWithArrayTest()
        {
            VelocityContext context = new VelocityContext();
            Template template = this.engine.GetTemplate(Common.ARRAY_TEMPLATE);

            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb);

            DateTime now = DateTime.Now;

            context.Put("string_array", new object[] { "John", "Smith", "hibernate_lib_style", now });

            template.Merge(context, writer);

            var expected = String.Format(
                Common.ARRAY_TEMPLATE_EXPECTED,
                now);
            Assert.AreEqual(expected, sb.ToString());
        }

        [Test]
        public void TemplateMergeTestWithGenericList()
        {
            VelocityContext context = new VelocityContext();
            Template template = this.engine.GetTemplate(Common.ARRAY_TEMPLATE);

            StringBuilder sb = new StringBuilder();
            TextWriter writer = new StringWriter(sb);

            DateTime now = DateTime.Now;

            context.Put("string_array", new List<string>() { "John", "Smith", "hibernate_lib_style", now.ToString() });

            template.Merge(context, writer);

            var expected = String.Format(
                "<John><Smith><hibernate_lib_style><{0}>",
                now);
            Assert.AreEqual(expected, sb.ToString());
        }

        //[Test]
        public void RuntimeConstantsTest()
        {
            Console.WriteLine(RuntimeConstants_Fields.RESOURCE_LOADER);
            Console.WriteLine(RuntimeConstants_Fields.RESOURCE_MANAGER_CLASS);
            Console.WriteLine(RuntimeConstants_Fields.RESOURCE_MANAGER_CACHE_CLASS);
            Console.WriteLine(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH);
        }
    }
}
